package com.saifan.wyy_ov.utils;

/**
 * @author WangLi
 * @date 2014年6月24日 下午5:00:08
 * @Description Acitivity栈用来管理所有的Activity
 * @version V1.0
 */

import android.app.Activity;

import com.socks.library.KLog;

import java.util.ConcurrentModificationException;
import java.util.Stack;

import lomasky.ma.httpUtils.JsonUtil;

public class ActivitysManager {
    private static Stack<Activity> activityStack;
    private static ActivitysManager instance;

    private ActivitysManager() {
    }

    public static ActivitysManager getManager() {
        if (instance == null) {
            instance = new ActivitysManager();
        }
        return instance;
    }

    // 退出栈顶Activity
    public void removeActivity() {
        Activity activity = activityStack.lastElement();
        if (activity != null) {
            activity.finish();
            activity = null;
        }
    }

    // 将Activity移除
    public void removeActivity(Activity activity) {

        if (activity != null) {
            activity.finish();
            activityStack.remove(activity);
            activity = null;
        }

    }

    // 将Activity移除
    @SuppressWarnings("rawtypes")
    public void removeActivityClass(Class activityClass) {
//        while (true) {
//            Activity activity = currentActivity();
//            if (activity == null) {
//                return;
//            }
//            if (activity.getClass().equals(activityClass)) {
//                removeActivity(activity);
//                return;
//            }
//        }

        try {
            if (activityStack == null|| activityStack.size()==0){
                return;
            }

            for (Activity activity : activityStack) {
                LogUtil.e(activityStack.toString()+"------"+ activityStack.size());
                LogUtil.e(activity.getClass().toString());
                if(activity.getClass().equals(activityClass) ){
                    removeActivity(activity);
                }
            }
        }catch (ConcurrentModificationException e){
            e.printStackTrace();
        }

    }

    // 获得当前栈顶Activity
    public Activity currentActivity() {
        Activity activity = null;
        if (!activityStack.empty())
            activity = activityStack.lastElement();
        return activity;
    }

    // 将当前Activity推入栈中
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    // 退出栈中所有除了cls的Activity
    @SuppressWarnings("rawtypes")
    public void removeAllActivityExceptOne(Class cls) {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(cls)) {
                break;
            }
            removeActivity(activity);
        }
    }

    // 退出栈中所有Activity
    public void removeAllActivity() {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            removeActivity(activity);
        }
    }

    public void exit() {
        for (Activity activity : activityStack) {
            if (activity!=null)
            activity.finish();
        }
        android.os.Process.killProcess(android.os.Process.myPid());    //获取PID
        System.exit(0);   //常规java、c#的标准退出法，返回值为0代表正常退出
    }
}